package graphix;

import java.awt.Color;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;

/**
 * @author duncan
 */
public class Box
{
    /**
     * Paints a box with a border, inside and a text.
     *
     * @param g The graphics-object to paint on.
     * @param x Where to paint the box.
     * @param y Where to paint the box.
     * @param width The width of the box.
     * @param height The height of the box.
     * @param text The text to be displayed in the box.
     * @param style The style to use on the box.
     */
    public static Graphics paint(Graphics g, int x, int y, int width, int height, String text, BoxStyle style, boolean hover)
    {
	height -= 1;

	//Draw the background if supposed to.
	if (style.showBackground)
	{
	    if (hover)
		g.setColor(style.color_background_hover);
	    else
		g.setColor(style.color_background);

	    g.fillRect(x, y, width, height + 1);
	}

	//Draw the background-image if supposed to.
	if (style.showBGImage)
	{
	    if (hover)
	    {
		g.drawImage(style.background_image_hover, x, y, width, height + 1, null);
	    }
	    else
	    {
		g.drawImage(style.background_image, x, y, width, height + 1, null);
	    }
	}

	//Draw the text if supposed to.
	if (style.showText)
	{
	    BufferedImage i = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
	    i.getGraphics().setColor(style.color_text);
	    i.getGraphics().setFont(style.font);
	    FontMetrics fm = i.getGraphics().getFontMetrics(style.font);
	    Rectangle2D textsize = fm.getStringBounds(text, i.getGraphics());
	    i.getGraphics().drawString(text,
		    width / 2 - ((int) textsize.getWidth() / 2),
		    height / 2 + ((int) textsize.getHeight()) / 2 - 2);
	    g.drawImage(i, x, y, null);
	}

	//Draw the border if supposed to.
	if (style.showBorder)
	{
	    g.setColor(style.color_border);
	    g.drawRect(x, y, width, height + 1);
	}

	//Return the new Graphics object with the box painted on it.
	return g;
    }
}
